<!--
* @FileDescription: 角色分配用户
* @Author: zhangjing
* @Date: 2021-9-28 3:27:32 ?F10: PM?
* @LastEditors: zhangjing
* @LastEditTime: 2021-9-28 3:27:32 ?F10: PM?
 -->
<template>
	<globalModal :title="title" :width="1200" :visible="visible" :confirmLoading="confirmLoading" @ok="handleOk" @cancel="handleCancel">
		<a-spin :spinning="confirmLoading">
			<filterbar @handlerSearch="handlerSearch" @handlerReset="handlerSearch" :field-list="superQueryFieldList"></filterbar>
			<s-table
				class="mt10"
				bordered
				:columns="columns"
				:data="loadListData"
				rowKey="userId"
				ref="table"
				:scroll="{ x: 700 }"
				size="small"
				:isFirstLoad="false"
				showPagination="auto"
				:row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
			></s-table>
		</a-spin>
	</globalModal>
</template>

<script>
import { sysManageapi } from '@/api/index.js';
import { ModalMixins } from '@/mixins';
import { clearDeep } from '@/utils/base';
import { SEXTYPE } from '@/utils/enumeration';
const columns = [
	{
		title: '所属机构',
		dataIndex: 'orgName',
		key: 'orgName',
		searchInfo: {
			value: 'orgId',
			type: 'select',
			defaultValue: undefined,
			optionlist: []
		}
	},
	{
		title: '账户',
		dataIndex: 'username',
		key: 'username'
	},
	{
		title: '姓名',
		dataIndex: 'realName',
		key: 'realName'
	},
	{
		title: '部门名称',
		dataIndex: 'deptName',
		key: 'deptName'
	}
];
export default {
	name: 'roleJoinUserModal',
	data() {
		return {
			title: '选择授权用户',
			columns,
			superQueryFieldList: [
				{
					type: 'String',
					text: '账户',
					value: 'username',
					defaultValue: null
				},
				{
					type: 'String',
					text: '姓名',
					value: 'name',
					defaultValue: null
				}
			],
			filter: {},
			selectedRowKeys: [],
			roleId: ''
		};
	},
	mixins: [ModalMixins],
	created() {},
	methods: {
		show(roleId) {
			this.visible = true;
			this.roleId = roleId;
			this.$nextTick(() => {
				this.handlerSearch();
			});
		},
		close() {
			this.$emit('close');
			this.visible = false;
		},
		handleOk() {
			if (this.selectedRowKeys.length == 0) {
				this.$message.error('请至少选中一个用户');
				return false;
			}
			this.confirmLoading = true;
			this.$postAction(sysManageapi.roleUrl.setRoleUser, {
				roleId: this.roleId,
				userIds: this.selectedRowKeys
			})
				.then(res => {
					if (res && res.code == 1) {
						this.$message.success('保存成功');
						this.$emit('ok');
						this.close();
					}
				})
				.finally(() => {
					this.confirmLoading = false;
				});
		},

		handleCancel() {
			this.close();
		},
		handlerSearch(filter) {
			this.selectedRowKeys = [];
			/* 过滤掉空对象 */
			clearDeep(filter);
			this.filter = { ...filter };
			this.$refs.table.refresh();
		},
		loadListData(parameter) {
			return this.$postAction(sysManageapi.roleUrl.getBindUser, {
				pageNum: parameter.pageNum,
				pageSize: parameter.pageSize,
				roleId: this.roleId,
				bindStatus: false,
				status: 0, //查询启用的账户
				...this.filter
			}).then(res => {
				return res.data;
			});
		},
		onSelectChange(selectedRowKeys) {
			this.selectedRowKeys = selectedRowKeys;
		}
	}
};
</script>

<style scoped></style>
